package com.example.home.mvvm.fragment
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.launcher.ARouter
import com.example.home.R
import com.example.home.mvvm.viewmodel.HomeViewModel
import com.gsafety.library.Utils.Log
import com.gsafety.libraryktx.base.BaseFragment
import com.gsafety.libraryktx.bean.LoadState
import com.gsafety.libraryktx.config.Constant
import com.gsafety.libraryktx.config.Constant.Companion.HAS_DEAL_CODE
import com.gsafety.libraryktx.config.Constant.Companion.IN_DEAL_CODE
import com.gsafety.libraryktx.config.Constant.Companion.NO_DEAL_CODE
import com.gsafety.libraryktx.core.RouterConstants
import com.gsafety.libraryktx.expand.create
import com.gsafety.libraryktx.manager.UserManager
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.home_fragment_layout.*
import kotlin.concurrent.timer

class HomeFragment : BaseFragment(),View.OnClickListener {
     private lateinit var mv: HomeViewModel
     var alarmTabTitleArray = Array<String>(3,{"";"";""})

     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         initmodel()
     }

    override fun initView(view: View?) {
        initcode()
        cl_un_deal.setOnClickListener(this)
        cl_in_deal.setOnClickListener(this)
        cl_finish_deal.setOnClickListener(this)
        cl_knowledge.setOnClickListener(this)
        rl_attention.setOnClickListener(this)
        cl_tongji.setOnClickListener(this)
    }

    override fun initData() {
    }

    override fun getLayoutId(): Int {
        return R.layout.home_fragment_layout
    }


   private  fun initcode(){
         var codes= UserManager.getInstance().appModelCodes()
         codes?.also {
             //今日预警
             if(!it.contains(Constant.CODE_TODAY_WARNING))  {
                 cl_notice.visibility=View.INVISIBLE
             }
             //待处理
             if(!it.contains(Constant.CODE_UNHANDLE)){
                 cl_un_deal.visibility=View.INVISIBLE
             }
             //处理中
             if(!it.contains(Constant.CODE_HANDING)){
                 cl_in_deal.visibility=View.INVISIBLE
             }
             //已完成
             if(!it.contains(Constant.CODE_COMPLETED)){
                 cl_finish_deal.visibility=View.INVISIBLE
             }
             //我的关注
             if(!it.contains(Constant.CODE_HOME_WDGZ)){
                 rl_attention.visibility=View.INVISIBLE
             }
             //知识库
             if(!it.contains(Constant.CODE_HOME_KNOWLEDGE)){
                 cl_knowledge.visibility=View.INVISIBLE
             }
             //统计分析
             if(!it.contains(Constant.CODE_HOME_TJ)){
                 cl_tongji.visibility=View.INVISIBLE
             }

         }
     }


     override fun onResume() {
         super.onResume()
         mv.requesthome()
     }


   private fun initmodel(){
       mv = HomeViewModel()
       mv = create(HomeViewModel::class)
       mv.loadState.observe(this, Observer {
           when (it) {
               is LoadState.Success -> {
                   var data=mv.data.value
                   data?.also {
                       tv_num.text=data.todayStatis
                       tv_un_deal_num.text=data.status0
                       tv_in_deal_num.text=data.status1
                       tv_finish_deal_num.text=data.status2
                       tv_attention_num.text="${data.myAttation}条"
                       alarmTabTitleArray[0] = data.status0
                       alarmTabTitleArray[1] = data.status1
                       alarmTabTitleArray[2] = data.status2
                       LiveEventBus.get(Constant.EventBus_KEY_Alarm_Title).post(alarmTabTitleArray)
                       LiveEventBus.get(Constant.EventBus_KEY_Message_Num).post(data.unread)
                   }
               }
               is LoadState.Fail -> {
               }

           }
       })
     }

     override fun onClick(v: View) {
         when(v.id){
             R.id.cl_un_deal->{
                 LiveEventBus.get(Constant.EventBus_KEY_Alarm_Deal_Click).post(NO_DEAL_CODE)
             }
             R.id.cl_in_deal->{
                 LiveEventBus.get(Constant.EventBus_KEY_Alarm_Deal_Click).post(IN_DEAL_CODE)
             }
             R.id.cl_finish_deal->{
                 LiveEventBus.get(Constant.EventBus_KEY_Alarm_Deal_Click).post(HAS_DEAL_CODE)
             }
             R.id.cl_knowledge->{
                 LiveEventBus.get(Constant.EventBus_KEY_Know_Switch).post("know")
             }
             R.id.rl_attention->{
                 ARouter.getInstance().build(RouterConstants.MY_ATTENTION_LIST_ACTIVITY).navigation()
             }
             R.id.cl_tongji->{
                 ARouter.getInstance().build(RouterConstants.ANALYSE_ACTIVITY).navigation()
             }
         }
     }

 }